package com.utrailer.common.rest;

import lombok.Data;

import static com.utrailer.common.rest.ResultCode.*;

/**
 * TODO
 *
 * @author Chen Song
 * @since V1.0
 */
@Data
public class ResultVO<T> {

    public static final ResultVO<?> GLOBAL_SUCCESS = of(SUCCESS.getCode(), SUCCESS.getMessage(), null);

    public static final ResultVO<?> GLOBAL_ERROR = of(ERROR.getCode(), ERROR.getMessage(), null);

    private String code;

    private String message;

    private T data;

    public static <T> ResultVO<T> of(String code, String msg, T data) {
        ResultVO<T> result = new ResultVO<>();
        result.setCode(code);
        result.setMessage(msg);
        result.setData(data);
        return result;
    }

    public static <T> ResultVO<T> success(T data) {
        ResultVO<T> result = new ResultVO<>();
        result.setCode(SUCCESS.getCode());
        result.setMessage(SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> ResultVO<T> error(String msg) {
        ResultVO<T> result = new ResultVO<>();
        result.setCode(ERROR.getCode());
        result.setMessage(msg);
        return result;
    }

    public static <T> ResultVO<T> error(ResultCode rc) {
        ResultVO<T> result = new ResultVO<>();
        result.setCode(rc.getCode());
        result.setMessage(rc.getMessage());
        return result;
    }

}
